< previous page page_445 next page >

Page 445
The preceding code example, when placed in the Load event of a form, causes the form's background color to change every time you click it.
Using Fonts and Colors with a RichTextBox Control
The text box is great for general use, but the RichTextBox control allows even greater control over how text is displayed. The strength of the RichTextBox control, introduced in Chapter 6, "Giving More Control to the User: Menus and Toolbars," is that it can display multiple formats and colors within the same text area.
sarrow.gifSee "Rich Text Boxes," p. 445
One use of this control can be to display database detail information in a neatly formatted manner, as in the application in Figure 19.12. When a user clicks an employee's name, the title and other information are displayed in the rich text box on the right of the form.
Although the information is displayed in a fancy format, the programming effort behind it is minimal. The first step is to use WordPad or Microsoft Word to create a template for your detail display. Enter what you want the data to look like, but use placeholders instead of actual data. A sample template is shown in Figure 19.12. Note that the # characters are used to surround each placeholder so that it is not confused with actual data.
0445-01.jpg
Figure 19.12
The formatted RTF template file has to be created
only once, and then it can be easily used to display data.
To display information in the rich text box, you must first load the template and then find and replace each placeholder. The following sample code uses a user-defined function, ReplRTBfield, to search for a placeholder and then replace it with data:
Sub ReplRTBfield(sField As String, sValue As String)
    rtbInfo.Find "#" & sField & "#"
    rtbInfo.SelText = sValue

End Sub

Private Sub cmdDisplay_Click()

   rtbInfo.LoadFile (App.Path & "\info.rtf")
   ReplRTBfield "NAME", "Brian Siler"
   ReplRTBfield "DEPT", "Accounting"
   ReplRTBfield "TITLE", "CIA"

End Sub
Although the code in this example is simple, you could easily expand it into a powerful formatting tool. The Find method of the rich text box allows you to replace your own codes in an RTF template instead of selecting and formatting each individual piece of text with VB code.

 
< previous page page_445 next page >